﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XUtility
{
    public static class LongEx
    {
        /// <summary>
        /// 返回正常格式的时间
        /// </summary>
        /// <param name="d">Unix时间</param>
        /// <param name="v">转换失败返回的默认值</param>
        /// <returns>DateTime类型</returns>
        public static DateTime ToDateTime(this long d, DateTime? v = null)
        {
            try
            {
                var str = d.ToString().PadRight(13, '0');
                var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                var time = startTime.AddMilliseconds(long.Parse(str));
                return time;
            }
            catch
            {
                if (v != null)
                {
                    return v.Value;
                }
                return DateTime.MinValue;
            }
        }
    }
}
